# -*- coding:utf-8 -*-


import tornado.web


class route(object):
    """
    decorates RequestHandlers and builds up a list of routables handlers

    Tech Notes (or 'What the *@# is really happening here?')
    --------------------------------------------------------

    Everytime @route('...') is called, we instantiate a new route object which
    saves off the passed in URI.  Then, since it's a decorator, the function is
    passed to the route.__call__ method as an argument.  We save a reference to
    that handler with our uri in our class level routes list then return that
    class to be instantiated as normal.

    Later, we can call the classmethod route.get_routes to return that list of
    tuples which can be handed directly to the tornado.web.Application
    instantiation.

    Example
    -------

    @route('/some/path')
    class SomeRequestHandler(RequestHandler):
        pass

    @route('/some/path', name='other')
    class SomeOtherRequestHandler(RequestHandler):
        pass

    my_routes = route.get_routes()
    """
    _routes = []

    def __init__(self, uri, name=None):
        self._uri = uri
        self.name = name

    def __call__(self, _handler):
        """ gets called when we class decorate
        """

        self._routes.append({"uri": self._uri, "name": self.name, "handler": _handler})
        return _handler

    @classmethod
    def add_app_route(cls, app_name, uri, name, handler):
        if not app_name in cls._routes:
            cls._routes[app_name] = []
        cls._routes[app_name].append({"uri": uri, "name": name, "handler": handler})

    @classmethod
    def get_routes(cls):
        import service
        routes = []
        for handler_dic in cls._routes:
            routes.append(tornado.web.url(handler_dic['uri'], handler_dic['handler'], name=handler_dic['name']))
        return routes

